package com.study.web.check;

import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author study
 * @version 1.0
 * @date 2020/9/21 20:46
 */
@Controller
@RequestMapping("/checks")
public class CheckController {

    @GetMapping("")
    public String index() {
        return "check";
    }


    List<Check> list = new ArrayList<Check>() {
        {
            add(new Check("A", "0101", "01第一", false));
            add(new Check("A", "0102", "01第二", true));
            add(new Check("B", "0201", "02第一", false));
            add(new Check("B", "0202", "02第二", true));
            add(new Check("B", "0203", "02第三", false));
            add(new Check("B", "0204", "02第四", true));
        }
    };

    @GetMapping("data")
    @ResponseBody
    public Map<String, Object> check(ModelMap modelMap) {
        Map<String, Object> result = new HashMap<>();
        result.put("date", new DateTime().toString());
        //分组
        Map<String, List<Check>> collectMap = list.stream().collect(Collectors.groupingBy(t -> {
            return t.getType();
        }));
        result.putAll(collectMap);
        System.out.println(collectMap);
        return result;
    }

    class Check {
        private String type;
        private String code;
        private String name;
        private boolean status = false;

        public boolean isStatus() {
            return status;
        }

        public void setStatus(boolean status) {
            this.status = status;
        }

        public Check(String type, String code, String name, boolean status) {
            this.type = type;
            this.code = code;
            this.name = name;
            this.status = status;
        }

        @Override
        public String toString() {
            return "Check{" +
                    "type='" + type + '\'' +
                    ", code='" + code + '\'' +
                    ", name='" + name + '\'' +
                    ", status=" + status +
                    '}';
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
